#include "Item.h"

#include "Hero.h"
#include "Balloon.h"
#include "HudLayer.h"


Item::Item()
{
}

Item::~Item()
{

}

void Item::initGameObjectWith(GamePlayScreen *_gameScreen, CCDictionary *_dict)
{
    PhysicObject::initGameObjectWith(_gameScreen, _dict);

    this->isActive = true;

    this->createSprite();
    this->createPhysicBody();
}

void Item::createSprite()
{
    type = (GAME_ITEM)dataDict->valueForKey(KEY_TYPE)->intValue();

    sprite = new CCSprite();
    sprite->initWithSpriteFrameName(CCString::createWithFormat("item_%d.png", type)->getCString());
    sprite->setPosition(getPosFromDict(dataDict, sprite->getContentSize()));
    sprite->setTag(ITEM);
    objType = ITEM;
}

void Item::createPhysicBody()
{
    b2BodyDef bodyDef;
    bodyDef.type = b2_staticBody;

    bodyDef.userData = this;
    if (sprite) {
        bodyDef.position.Set(sprite->getPositionX() / PTM_RATIO, sprite->getPositionY() / PTM_RATIO);
    }
    physicBody = handler->world->CreateBody(&bodyDef);

    DataAccess::sharedDataAccess()->getFixturesForBody(physicBody, CCString::createWithFormat(
                                                           "item%d", type)->getCString(), 0);
}

void Item::addToRemoveList(CCObject *sender)
{    
	if (this->heroIndex == handler->mHero->playerOrder) {

    }
    if (this->handler->isHost) {
        this->handler->addToRemoveList(this);
    }
}

void Item::changeState(unsigned char _state)
{
    if (objState == _state) {
        return;
    }
    objState = _state;
    this->sprite->stopAllActions();

    if (objState == ST_IT_NORMAL) {
        return;
    }
    else if (objState == ST_IT_EATEN) {
        this->isActive = false;

        if (this->heroIndex != handler->mHero->playerOrder) {
            this->sprite->setVisible(false);
        }

        CCParticleSystemQuad *eaten = CCParticleSystemQuad::create("item_collected.plist");
        eaten->setPosition(this->sprite->getPosition());
        this->handler->addChild(eaten, Z_EFFECT);

        CCPoint desPos = CCPointMake(handler->hudLayer->lifeLabel->getPositionX() - handler->getPositionX(),
                                     handler->hudLayer->lifeLabel->getPositionY() - handler->getPositionY());
        if (this->type == IT_POINT) {
//            desPos = CCPointMake(handler->hudLayer->scoreLabel->getPositionX() - handler->getPositionX(),
//                                 handler->hudLayer->scoreLabel->getPositionY() - handler->getPositionY());
        } else {
//            desPos = CCPointMake(handler->hudLayer->scoreLabel->getPositionX() - handler->getPositionX(),
//                                 handler->hudLayer->scoreLabel->getPositionY() - handler->getPositionY());
        }
		
        ///*/ item fly to position
		this->sprite->runAction(CCSequence::create(
                                    CCEaseIn::create(CCMoveTo::create(0.2f, desPos), 0.5f),
									CCCallFuncN::create(this, callfuncN_selector(Item::addToRemoveList)),
									//CCCallFunc::create(this, callfunc_selector(Item::addToRemoveList)),
									NULL
                                    )
                                );
    }
}

void Item::beginContact(b2Fixture *ownFixture, b2Fixture *otherFixture, b2Contact *contact)
{
    PhysicObject *physicObj = (PhysicObject*)otherFixture->GetBody()->GetUserData();
    GAME_OBJECT otherType = physicObj->objType;

    if (otherType == HERO && ((Hero*)physicObj)->mBalloon->isBalloon) {
        if (isActive) {
            this->heroIndex = ((Hero*)physicObj)->playerOrder;
            this->changeState(ST_IT_EATEN);
            isActive = false;
        }
    }

    PhysicObject::beginContact(ownFixture, otherFixture, contact);
}
